﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Core;

namespace SharedGenomics.Workbench.PresentationModel
{
    public class DataColumnSchema : IEnumerable<DataGridColumnInfo>
    {
        public DataColumnSchema()
        {
            this.Columns = new List<DataGridColumnInfo>();
            this.PropertyNameLookup = new Dictionary<string, DataGridColumnInfo>();
        }

        public virtual void Add(DataGridColumnInfo column)
        {
            column.ThrowIfNull("column");
            ExceptionHelper.ThrowIf<ArgumentException>(this.PropertyNameLookup.ContainsKey(column.PropertyName), String.Format("A column for property {0} has already been added", column.PropertyName));

            column.Index = this.Columns.Count;
            this.Columns.Add(column);
            this.PropertyNameLookup.Add(column.PropertyName, column);
        }

        public DataGridColumnInfo this[int index]
        {
            get
            {
                index.ThrowIfNotBetween(0, this.Columns.Count - 1, true, "index");
                return this.Columns[index];
            }
        }

        public DataGridColumnInfo this[string propertyName]
        {
            get
            {
                bool columnExists = propertyName != null && this.PropertyNameLookup.ContainsKey(propertyName);
                ExceptionHelper.ThrowIf<ArgumentException>(!columnExists, String.Format("No column exists for property name {0}", propertyName ?? "<null>"));

                return this.PropertyNameLookup[propertyName];
            }
        }

        private IList<DataGridColumnInfo> Columns { get; set; }
        private IDictionary<string, DataGridColumnInfo> PropertyNameLookup { get; set; }

        public int Count
        {
            get { return this.Columns.Count; }
        }

        public IEnumerator<DataGridColumnInfo> GetEnumerator()
        {
            return this.Columns.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}
